package com.fy.tt.service.impl;

import com.fy.tt.action.TemplateTool;
import com.fy.tt.dao.EnvironmentDao;
import com.fy.tt.entity.Environment;
import com.fy.tt.entity.query.EnvironmentQuery;
import com.fy.tt.service.EnvironmentService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class EnvironmentServiceImpl implements EnvironmentService {
    Connection dbConnection = null;
    EnvironmentDao environmentDao = null;

    {
        try {
            dbConnection = TemplateTool.getDBConnection();
            environmentDao = TemplateTool.getBean(EnvironmentDao.class);
        } catch (Exception e) {
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }

    @Override
    public List<Environment> getAllEnvironments() {
        List<Environment> environments = null;
        try {
            environments = environmentDao.getAllEnvironments(dbConnection);
        } catch (Exception e) {

        }
        return environments;
    }

    @Override
    public int addEnvironment(Environment environment) {
        int i = -1;
        try {
            i = environmentDao.addEnvironment(dbConnection, environment);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteEnvironment(int id) {
        int i = -1;
        try {
            i = environmentDao.deleteEnvironment(dbConnection, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateEnvironment(Environment environment) {
        int i = -1;
        try {
            i = (int) environmentDao.updateIgnoreNull(dbConnection, environment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<Environment> getPageEnvironments(int pageSize, int current) {
        List<Environment> environments = null;
        try {
            environments = environmentDao.getPageEnvironments(dbConnection, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return environments;
    }

    @Override
    public List<Environment> getConditionPageEnvironments(EnvironmentQuery environmentQuery, int pageSize, int current) {
        List<Environment> environments = null;
        try {
            environments = environmentDao.getConditionPageEnvironments(dbConnection, environmentQuery, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return environments;
    }
}